package com.yuan.java.concurrency;

import org.springframework.util.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * httpHelper http请求工具类
 */
public class HttpHelper {

    public static String post(String url, Map<?, ?> params) throws IOException {
        String content = getParams(params);
        return doPost(url, content);
    }

    public static String post(String url) throws IOException {
        return doPost(url, "");
    }

    public static String get(String url) throws IOException {
        return doGet(url, new HashMap<>());
    }

    public static String doGet(String url, Map<?, ?> params) throws IOException {
        String param;
        String getURL = url;
        if (params.size() > 0) {
            param = getParams(params);
            getURL = getURL + "?" + param;
        }
        URL getUrl = new URL(getURL);
        HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
        connection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }
        reader.close();
        connection.disconnect();
        return stringBuilder.toString();
    }

    private static String getParams(Map<?, ?> params) {
        StringBuilder sb = new StringBuilder();
        params.forEach((K, V) -> {
            sb.append("&");
            sb.append(K.toString());
            sb.append("=");
            sb.append(V.toString());
        });
        if (sb.length() > 0) {
            sb.deleteCharAt(0);
        }
        try {
            return URLEncoder.encode(sb.toString(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String doPost(String url, String params) throws IOException {
        URL postUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.connect();
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        if (!StringUtils.isEmpty(params)) {
            out.writeBytes(params);
        }
        out.flush();
        out.close();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }
        reader.close();
        connection.disconnect();
        return stringBuilder.toString();
    }
}
